/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package TMS;

/**
 *
 * @author Zachary
 */
public class Treadmill {
    public static final double max_speed = 15;
    public static final double max_incline = 20;
    private static double current_speed = 0;
    private static double current_incline = 0;
    private static double target_speed = 0;
    private static double target_incline = 0;
    private static boolean changing_speed = false;
    private static boolean changing_incline = false;
    
    //Set Speed/Incline directly
    public static void set_speed(double value){
        if (value > 0 && value <= max_speed){
            target_speed = value;
            if (changing_speed == false) new SpeedChanger().start();
        }
    }
    public static void set_incline(double value){
        if (value >= 0 && value <= max_incline){
            target_incline = value;
            if (changing_incline == false) new InclineChanger().start();
        }
    }

    //
    //Bring the treadmill to a halt.
    //
    public static void kill(){
        //Return speed and incline to 0
        target_speed = 0;
        target_incline = 0;
        current_speed = 0;
        current_incline = 0;
    }
    /* Auxilary Classes owned by treadmill */
    //SpeedChanger:
    //  Changes speed over time. Changes follow
    //  an 'S' curve.
    //
    private static class SpeedChanger extends Thread{
        
        public SpeedChanger(){
            
        }
        @Override
        
        public void run(){
            changing_speed = true;
            while (current_speed != target_speed){
                
                if (target_speed < current_speed) current_speed -= .1;
                else current_speed += .1;
                Clock.wait(.1);
                if (Math.abs(target_speed-current_speed) < .001) break;
            }
            current_speed = target_speed;            
            changing_speed = false;
        }        
    }
    //InclineChanger:
    //  Changes incline over time. Changes follow
    //  an 'S' curve.
    //
    private static class InclineChanger extends Thread{
        
        public InclineChanger(){
            
        }        
        @Override
        public void run(){
            changing_incline = true;
            while (current_incline != target_incline){
                if (target_incline < current_incline) current_incline -= .1;
                else current_incline += .1;
                Clock.wait(.1);
                if (Math.abs(target_incline - current_incline) < .001) break;
            }
            current_incline = target_incline;
            changing_incline = false;
        }
    }
    //
    //Accessors:
    //
    public static double get_current_speed(){
        return current_speed;
    }
    public static double get_current_incline(){
        return current_incline;
    }
    
}
